<?php
/**
 * communit.as
 * @copyright (C)2008 J. William McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author J. William McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 *
 * @package communitas
 * @subpackage cms_models
 * @license http://communit.as/docs/license License 
 */

class Cms_Pages extends App_Db_Table_Abstract
{
    protected $_name 	= 'cms_pages';
    protected $_primary = 'id';
    
    function _setup(){
    	parent::_setup();
    	$this->_wp_username = App_Registry::Get('wordpress_username','');
    	$this->_wp_password = App_Registry::Get('wordpress_password','');
    	$this->_wp_url = App_Registry::Get('wordpress_xmlrpc_url','');
    }
    
    function getBySlug($slug){
    	$where = $this->getAdapter()->quoteInto('slug = ?',$slug);    	
    	return $this->fetchRow($where);
    }
    
	function syncPosts(){
		$client = new Zend_XmlRpc_Client($this->_wp_url);
		try{
			$posts_remote = $client->call('metaWeblog.getRecentPosts',array(null,$this->_wp_username,$this->_wp_password,999999));
			parent::delete("type = 'post'");			
			foreach($posts_remote as $post){
				$data = array(
					'id' 					=> $post['postid'],
					'slug' 					=> $post['wp_slug'],
					'type' 					=> 'post',
					'posted_on' 			=> date(DB_DATETIME_FORMAT,strtotime($post['dateCreated'])),
					'status' 				=> 'publish',
					'title' 				=> $post['title'],
					'text' 					=> $post['description'],
					'author_id' 			=> $post['wp_author_id'], 
					'author_display_name' 	=> $post['wp_author_display_name'],				
					'parent_id' 			=> null,
					'parent_title' 			=> null,
					'page_order' 			=> null,
				);
				
				parent::insert($data);
			}
			
		} catch(Exception $ex) {
			throw($ex);
		}	
	}    

	function syncPages(){
		$client = new Zend_XmlRpc_Client($this->_wp_url);
		try{
			$pages_remote = $client->call('wp.getPages',array(null,$this->_wp_username,$this->_wp_password));			
			parent::delete("type = 'page'");
			foreach($pages_remote as $page){
				$data = array(
					'id' 					=> $page['page_id'],
					'slug' 					=> $page['wp_slug'],
					'type' 					=> 'page',
					'posted_on' 			=> date(DB_DATETIME_FORMAT,strtotime($page['dateCreated'])),
					'status' 				=> $page['page_status'],
					'title' 				=> $page['title'],
					'text' 					=> $page['description'],
					'author_id' 			=> $page['wp_author_id'], 
					'author_display_name' 	=> $page['wp_author_display_name'],				
					'parent_id' 			=> $page['wp_page_parent_id'],
					'parent_title' 			=> $page['wp_page_parent_title'],
					'page_order' 			=> $page['wp_page_order'],
				);
			
				parent::insert($data);
			}
			
		} catch(Exception $ex) {
			throw($ex);
		}	
	}

}

